//
//  WelcomeViewController.swift
//  CHT
//
//  Created by aiquantong on 2019/2/19.
//  Copyright © 2019 difan. All rights reserved.
//

import UIKit
import AlamofireImage

class WelcomeViewController: BasicViewController {

     @IBOutlet var imageView: UIImageView!
     @IBOutlet var adImageView: UIImageView!
    
     @IBOutlet var timeLabel: UILabel!
     @IBOutlet var timeView: TimerView!
    
    var index : Float = 0.0;
    let maxIndex : Float = 3.2;
    var timer : Timer!;
    
    deinit {
        NotificationCenter.default.removeObserver(self);
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        let all:Int = Int(UIScreen.main.bounds.size.width*UIScreen.main.bounds.size.height);
//        var imageName:String = "LaunchImage-700@2x.png";
//        if all <= 320*480 {
//            imageName = "LaunchImage-700@2x.png";
//        }else if all <= 320 * 568 {
//            imageName = "LaunchImage-700-568h@2x.png";
//        }else if all <= 375*667 {
//            imageName = "LaunchImage-800-667h@2x.png";
//        }else if all <= 414*736 {
//            imageName = "LaunchImage-800-Portrait-736h@2x.png";
//        }else{
//            imageName = "LaunchImage-1100-Portrait-2436h@3x.png";
//        }
        
        let viewOr = "Portrait"//垂直
        var launchImageName = ""
        let viewSie = UIScreen.main.bounds.size
        let tmpLaunchImages = Bundle.main.infoDictionary!["UILaunchImages"] as? [Any]
        for dict in tmpLaunchImages! {
            if let someDict = dict as? [String: Any] {
                let imageSize = NSCoder.cgSize(for:someDict["UILaunchImageSize"] as! String)
                if __CGSizeEqualToSize(viewSie, imageSize) && viewOr == someDict["UILaunchImageOrientation"] as! String {
                    launchImageName = someDict["UILaunchImageName"] as! String
                }
            }
        }
        
        imageView?.image = UIImage(named: launchImageName);
        
        initTimer()
        
        if AdvertManager.share().dtappScreenArr != nil &&
            AdvertManager.share().dtappScreenArr.count > 0 {
            self.setUpAdertise();
        }else{
            AdvertManager.share().requestAdvertContent();
            NotificationCenter.default.addObserver(self, selector: #selector(setUpAdertise), name: AppConfig.Notification.AdertUpdate, object: nil);
        }
        
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        self.navigationController?.setNavigationBarHidden(true, animated: animated);
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidAppear(animated);
        if timer != nil && timer.isValid {
            timer.invalidate()
        }
        self.navigationController?.setNavigationBarHidden(false, animated: animated);
    }

    func initTimer() -> Void {
        if timer != nil {
            timer.invalidate()
        }
        
        index = 0;
        showTimerVew();
        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(foTimer), userInfo: nil, repeats: true);
        timer.fire();
    }
    
   @objc func foTimer(_ mtimer:Timer) -> Void {
        index = index + 0.1;
        showTimerVew();
        
        if (index >= maxIndex + 0.1) {
            if timer != nil {
                timer.invalidate();
                timer = nil;
            }
            let ad : AppDelegate = UIApplication.shared.delegate as! AppDelegate;
            ad.hideWelcome();
        }
    }
    
    func showTimerVew() -> Void {
        timeLabel.text = "\(Int(index))秒"
        timeView.curVal = index;
        timeView.maxVal = maxIndex;
        
        timeView.setNeedsLayout();
        timeView.setNeedsDisplay();
        timeView.setNeedsUpdateConstraints();
    }

    @IBAction func skipAdd(_ sender: Any) {
        
        if timer != nil {
            timer.invalidate();
            timer = nil;
        }
        let ad : AppDelegate = UIApplication.shared.delegate as! AppDelegate;
        ad.hideWelcome();
    }

    @objc func setUpAdertise() -> Void {
        if AdvertManager.share().dtappScreenArr != nil &&
            AdvertManager.share().dtappScreenArr.count > 0 {
            let adModel : AdModel = AdvertManager.share().dtappScreenArr.firstObject as! AdModel;
            
            let imageurl : URL! = URL(string: adModel.AT_Image);
            self.adImageView.isHidden = false;
            self.adImageView.af_setImage(withURL: imageurl);
        
        }
    }

}
